home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / demo / demosrc / d_animate.pro next >
Text File  |  1997-07-08  |  11KB  |  327 lines

  1. ; $Id: d_animate.pro,v 1.16 1997/04/18 01:45:37 tremblay Exp $
  2. ;
  3. ;  Copyright (c) 1997, Research Systems, Inc. All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ;  FILE:
  8. ;       cfd.pro
  9. ;
  10. ;  CALLING SEQUENCE: cfd
  11. ;
  12. ;  PURPOSE:
  13. ;       Display an animation of the pressure field around a sphere.
  14. ;
  15. ;  MAJOR TOPICS: Animation and widgets
  16. ;
  17. ;  CATEGORY:
  18. ;       IDL 4.0
  19. ;
  20. ;  INTERNAL FUNCTIONS and PROCEDURES:
  21. ;       pro d_cfd                 - Call the cfd animation.
  22. ;       pro d_tides               - Call the tides animation.
  23. ;       pro d_gated               - Call the gated blood animation.
  24. ;       pro Animate_demo_Event    - Event handler
  25. ;       pro Animate_demo_Cleanup  - Cleanup
  26. ;       pro cfd                   - Main procedure
  27. ;
  28. ;  EXTERNAL FUNCTIONS, PROCEDURES, and FILES:
  29. ;       pro CW_ANIMATE:         - Animation tool routine
  30. ;       pro CW_ANIMATE_INIT:    - Animation tool routine
  31. ;       pro CW_ANIMATE_LOAD:    - Animation tool routine
  32. ;       pro CW_ANIMATE_RUN:     - Animation tool routine
  33. ;       fun getTips             - Get the tip text 
  34. ;       pro widTips             - Create the widget text for tips
  35. ;       pro sizeTips            - Size the widget text for tips
  36. ;
  37. ;  REFERENCE: IDL Reference Guide, IDL User's Guide
  38. ;
  39. ;  NAMED STRUCTURES:
  40. ;       none.
  41. ;
  42. ;  COMMON BLOCS:
  43. ;       none.
  44. ;
  45. ;  MODIFICATION HISTORY: 
  46. ;
  47. ;-
  48. ;---------------------------------------------------------
  49. ;
  50. ;  PURPOSE  :  Call for the pressure field animation.
  51. ;
  52. pro d_cfd, $
  53.          GROUP = group, $       ; IN: (opt) group identifier
  54.          APPTLB = wTopBase ; OUT: (opt) TLB of this application
  55.  
  56. d_animate, "cfd", "Pressure Field Animation", $
  57.   NFRAMES=16, NCOLORS=160, $
  58.   GROUP=group, APPTLB = wTopBase
  59. end                             ;cfd
  60.  
  61. ;---------------------------------------------------------
  62. ;
  63. ;  PURPOSE  :  Call for the oceanic tides animation.
  64. ;
  65. pro d_tides, $
  66.          GROUP = group, $       ; IN: (opt) group identifier
  67.          APPTLB = wTopBase ; OUT: (opt) TLB of this application
  68.  
  69. d_animate, "tides", "Oceanic Tides", $
  70.   NFRAMES=12, NCOLORS=150, $
  71.   GROUP=group, APPTLB = wTopBase
  72. end                             ;cfd
  73.  
  74. ;---------------------------------------------------------
  75. ;
  76. ;  PURPOSE  :  Call for the gated blood pool animation.
  77. ;
  78. pro d_gated, $
  79.          GROUP = group, $       ; IN: (opt) group identifier
  80.          APPTLB = wTopBase ; OUT: (opt) TLB of this application
  81.  
  82. ; If gated data file is NOT compressed:
  83. d_animate, "gated", "Gated Blood Pool", $
  84.   UNCOMPRESSED = [128, 64], $
  85.   COLOR_TABLE_INDEX = 3, $
  86.   NFRAMES=15, NCOLORS=225, GROUP=group, APPTLB = wTopBase, /ZOOM
  87.  
  88. ; If we use a gif file:
  89. ; animate_demo, "gated", "Gated Blood Pool", $
  90. ;  NFRAMES=15, NCOLORS=225, GROUP=group, APPTLB = wTopBase, /ZOOM
  91.  
  92. end                             ;cfd
  93.  
  94. ;---------------------------------------------------------
  95. ;
  96. ;  PURPOSE  : event handler
  97. ;
  98. pro Animate_Demo_Event, $
  99.        sEvent                   ; IN: event structure
  100.  
  101. if (TAG_NAMES(sEvent, /STRUCTURE_NAME) EQ $
  102.     'WIDGET_KILL_REQUEST') then begin
  103.     WIDGET_CONTROL, sEvent.top, /DESTROY
  104.     RETURN
  105. endif
  106.  
  107. WIDGET_CONTROL, sEvent.id, GET_UVALUE= uValue
  108.  
  109. case uValue of
  110.         ;  Quit this application (end animation button).
  111.         ;
  112.     0 : begin
  113.         WIDGET_CONTROL, sEvent.top, /DESTROY
  114.         RETURN
  115.     end
  116.  
  117.         ;  Quit this application (menu bar).
  118.         ;
  119.     1 : begin
  120.         WIDGET_CONTROL, sEvent.top, /DESTROY
  121.         RETURN
  122.     end
  123.  
  124.     2 : begin
  125.         XLOADCT, GROUP=sEvent.top
  126.         RETURN
  127.     end
  128.  
  129.         ;  Display the information file.
  130.         ;
  131.     3 : begin
  132.             ;  Verify that there is only one instance of XDisplayFile
  133.             ;
  134.         if (Xregistered('XDisplayFile') NE 0) then RETURN
  135.         Widget_Control, sEvent.top, GET_UVALUE = sInfo
  136.         XDisplayFile, filepath(sInfo.DemoName + ".txt", $
  137.                                SUBDIR=['examples','demo','demotext']), $
  138.           DONE_BUTTON='Done', $
  139.           TITLE= sInfo.Title, $
  140.           GROUP=sEvent.top, WIDTH=55, HEIGHT=14
  141.     end
  142.     
  143. endcase
  144. end   ; of Animate_Demo_EVENT
  145.  
  146. ;---------------------------------------------------------
  147. ;
  148. ;  PURPOSE  : Cleanup procedure
  149. ;
  150. pro Animate_Demo_Cleanup, wTopBase
  151.  
  152.     ;  Get the info structure saved in the window's user value.
  153.     ;
  154.     WIDGET_CONTROL, wTopBase, GET_UVALUE=sInfo, /NO_COPY
  155.  
  156.     ;  Restore the previous color table.
  157.     ;
  158.     TVLCT, sInfo.colorTable
  159.  
  160.     ;  Map the group leader bvase if it exists.
  161.     ;
  162.     if (WIDGET_INFO(sInfo.groupBase, /VALID_ID)) then $
  163.         WIDGET_CONTROL, sInfo.groupBase, /MAP
  164.  
  165. end
  166.  
  167. ;---------------------------------------------------------
  168. ;
  169. PRO d_animate, Demoname, Title, $
  170.            NFrames = Nframes, $
  171.            NCOLORS=ncolors, $   ;# of colors required, if omitted !d.n_colors
  172.            UNCOMPRESSED = uncompressed, $ ;If data file isn't in GIF format,
  173.                                 ;this contains the dimensions of the images. 
  174.            ZOOM=zoom, $         ;TRUE to zoom up if screen is large
  175.            COLOR_TABLE_INDEX = colortb_index, $ ; If set, load this color tbl
  176.            GROUP = group, $     ; IN: (opt) group identifier
  177.            APPTLB = wTopBase    ; OUT: (opt) TLB of this application
  178.  
  179. ; tstart = systime(1)
  180. if (n_elements(group) NE 0) then groupBase = group $
  181. else groupBase = 0L
  182.  
  183.     ;  Initialize the device.
  184.     ;
  185. if (((!D.Name EQ 'X') OR (!D.NAME EQ 'MacOS')) AND $
  186.     (!D.N_Colors GE 256L)) then DEVICE, PSEUDO_COLOR=8
  187. DEVICE, Decomposed=0, Bypass_Translation=0
  188. DEVICE, GET_SCREEN_SIZE = scrsize
  189.  
  190. drawbase = startmes(GROUP=groupbase) ;  Create the starting up message.
  191.  
  192. ; Determine the input file name:
  193. if keyword_set(uncompressed) then filename = DemoName + '.dat' $
  194. else filename = DemoName + '.gif'
  195. Filename = filepath(filename, SUBDIR=['examples', 'demo', 'demodata'])
  196.  
  197. openr, lun, /GET_LUN, Filename, ERROR=i ;See if file is readable
  198. if i lt 0 then begin
  199.     result = DIALOG_MESSAGE(["Can't read Data file:", filename], /ERROR)
  200.     if groupBase ne 0 then WIDGET_CONTROL, groupBase, /MAP
  201.     WIDGET_CONTROL, drawbase, /DESTROY ;  Destroy the starting up window.
  202.     RETURN
  203. endif
  204.  
  205. ;  Get the current color table. It will be restored when exiting.
  206. TVLCT, savedR, savedG, savedB, /GET
  207. colorTable = [[savedR],[savedG],[savedB]]
  208.  
  209. if keyword_set(uncompressed) then begin
  210.     Image = Bytarr(uncompressed(0), uncompressed(1), /NOZERO)
  211.     readu, Lun, Image
  212.     if n_elements(colortb_index) ne 0 then loadct, colortb_index, /SILENT
  213.     ncolors = !d.table_size     ;We've got the correct number of colors
  214.     tvlct, Red, Green, Blue, /GET
  215. endif else begin
  216.     read_gif, Filename, Image, Red, Green, Blue, /MULTIPLE ;Get the first image
  217.     free_lun, lun
  218. endelse
  219.  
  220. s = size(Image)                 ;Get dimensions of frame
  221. ImXSize = s(1)                  ;Window and file image size
  222. ImYSize = s(2)
  223.  
  224.  
  225. if keyword_set(ZOOM) then begin ;Rebin?
  226.     DEVICE, GET_SCREEN=screenSize ;How large is the screen
  227.     
  228. ;  Try for approx 1/2 the vertical height of the screen
  229.     zoomFactor = 0.666 * Float(screenSize) / s(1:2) ;Screensize / image size
  230.     zoomFactor = FLOOR(min(zoomFactor)) > 1 ;Vertical zoom
  231. endif else zoomFactor = 1
  232.  
  233. winxsize = zoomFactor * ImXSize
  234. winysize = zoomFactor * ImYSize
  235.  
  236. sText = getTips(filepath(DemoName+'.tip', $
  237.                          SUBDIR=['examples','demo', 'demotext']) )
  238.  
  239. if n_elements(ncolors) eq 0 then ncolors = !d.table_size ;Default = what we got
  240.  
  241. if !d.table_size lt ncolors then $ ;Compress color tables?
  242.   TVLCT, BYTSCL(red, MAX=ncolors-1), $
  243.   BYTSCL(green, MAX=ncolors-1), $
  244.   BYTSCL(Blue, MAX=ncolors-1) $
  245. else TVLCT, Red, Green, Blue         ;The Demo's color table
  246.  
  247. wTopBase = WIDGET_BASE(TITLE = Title, /COLUMN, $
  248.                        MBAR=barBase, $
  249.                        /TLB_KILL_REQUEST_EVENTS, $
  250.                        GROUP_LEADER=groupbase, $
  251.                        TLB_FRAME_ATTR=1, MAP=0)
  252.  
  253. wFileButton = WIDGET_BUTTON(barBase, VALUE='File', $
  254.                             UVALUE='File', /MENU)
  255.  
  256. wQuitButton = WIDGET_BUTTON(wFileButton, VALUE='Quit', $
  257.                             UVALUE=1)
  258.  
  259. wViewButton = WIDGET_BUTTON(barBase, VALUE='View', $
  260.                             UVALUE='VIEW', /MENU)
  261.  
  262. wColorButton = WIDGET_BUTTON(wViewButton, $
  263.                              VALUE='Colors...', $
  264.                              UVALUE=2)
  265.  
  266. wHelpButton = WIDGET_BUTTON(barBase, VALUE='About', $
  267.                             UVALUE='HELP', /MENU, /HELP)
  268.  
  269. wAboutButton = WIDGET_BUTTON(wHelpButton, $
  270.                              VALUE='About this animation', $
  271.                              UVALUE=3)
  272.  
  273. animate = CW_ANIMATE(wTopBase, winxsize, $
  274.                      winysize, Nframes,$
  275.                      INFO_FILE=filepath(DemoName + '.txt', $
  276.                                         SUBDIR=['examples','demo','demotext']))
  277.  
  278. wStatusBase = WIDGET_BASE(animate, MAP=0, /ROW) ;  text widget for tips.
  279.  
  280. nWidgets = 2
  281. wText = LONARR(nWidgets)
  282. widTips, wStatusBase, sText.text, XSIZE=36, YSIZE=3, NWIDGETS=nWidgets, wText
  283.  
  284. WIDGET_CONTROL, wTopBase, /REALIZE ;  Realize the widget hierarchy.
  285.  
  286. sizeTips, wTopBase, wText, wStatusBase ;  Size the tips widgets.
  287.  
  288.  
  289. sInfo = { $                     ;  Create the info structure.
  290.           DemoName : DemoName, $ ;our name
  291.           Title : Title, $
  292.           colorTable: colorTable, $ ; saved color table to restore
  293.           groupBase: groupbase $ ; Base of Group Leader
  294.         }
  295.  
  296. WIDGET_CONTROL, wTopBase, SET_UVALUE=sInfo, /NO_COPY
  297. WIDGET_CONTROL, drawbase, /DESTROY ;  Destroy the starting up window.
  298. WIDGET_CONTROL, wTopBase, map = 1 ;  Map the top level base.
  299.  
  300. for i = 0, Nframes-1 do begin  ;  Load the images.
  301.     if i ne 0 then begin            ;Read 2nd and following images
  302.         if keyword_set(uncompressed) then readu, lun, Image $
  303.         else read_gif, Filename, Image, Red, Green, Blue, /MULTIPLE
  304.     endif
  305.  
  306.     if !d.table_size lt ncolors then $ ;Compress color tables?
  307.       image = BYTSCL(image, TOP=!D.TABLE_SIZE-1, MAX=ncolors, MIN=0)
  308.  
  309.     if zoomFactor ne 1 then $   ;Zoom up?
  310.       CW_ANIMATE_LOAD, animate, FRAME=i, $
  311.         IMAGE= REBIN(Image, winxsize, winysize) $
  312.     else CW_ANIMATE_LOAD, animate, FRAME=i, image= image
  313. endfor
  314.  
  315. if keyword_set(uncompressed) then FREE_LUN, lun $
  316. else READ_GIF, /CLOSE           ;Done w/ input file
  317.  
  318. Image = 0                       ;Free the space
  319.  
  320. CW_ANIMATE_RUN, animate, 20     ; Run the animation...
  321.  
  322. ; print, systime(1) - tstart, ' seconds'
  323.  
  324. XMANAGER, DemoName, wTopBase, Event_Handler='animate_demo_Event', $
  325.   Cleanup='animate_demo_Cleanup', /NO_BLOCK
  326. end                             ; animate_demo
  327.